<?php

	require_once(dirname(dirname(dirname(dirname(__FILE__)))) . "/engine/start.php");
	gatekeeper();

	$session = get_entity(get_input("chatsession"));
	$maxmembers = get_entities_from_relationship("", $session->getGUID(), null, null, null, null, null, null, null, true);
	$members = get_entities_from_relationship("", $session->getGUID(), null, null, null, null, null, $maxmembers);
	$maxfriends = get_entities_from_relationship("friend", get_loggedin_userid(), null, null, null, null, null, null, null, true);
	$friends = get_entities_from_relationship("friend", get_loggedin_userid(), null, null, null, null, null, $maxfriends);
	
?>
		
	<script type="text/javascript">
		function addFriend(friend){
			$.post("<?php echo $CONFIG->wwwroot; ?>action/elggchat/invite?chatsession=<?php echo $session->guid;?>&friend=" + friend, function(){
			
				$("#friend_" + friend).fadeOut(500, function() { $(this).remove(); });
			});
		}
	</script>
	
	<h3 class='settings'><?php echo elgg_echo('elggchat:action:invite_friends');?></h3>
	<p>
		<?php
			foreach($friends as $friend){
				if(!is_member($friend->guid, $members)){
					echo "<div id='friend_" . $friend->guid . "'>";
					echo "<img class='messageIcon' alt='" . $friend->name . "' src='". $friend->getIcon('tiny') . "'>";
					echo "<a href='javascript:addFriend(" . $friend->guid . ");'>" . $friend->name . "</a><br />";
					echo "</div>";
				}
			}
		?>
	</p>
	<?php
		echo "<center>" .  elgg_view("input/button", array("type"=>"button","value"=>elgg_echo("close"),"js"=>"onclick='javascript:showChat(" . $session->guid . ");'")) . "</center>";		
	?>